package net.turkishpt;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.http.*;


@SuppressWarnings("serial")
public class Turkish_predictive_text_demoServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.sendRedirect("/ui.jsp");
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		resp.setContentType("text/html;charset=UTF-8");
		
		String frmKeystrokes = req.getParameter("frmKeystrokes").trim();
		StringTokenizer st = new StringTokenizer(frmKeystrokes, " ");
		
		ArrayList<Integer> keyNumbers = new ArrayList<Integer>();
		
		while(st.hasMoreTokens()){
			try{
				int intVal = Integer.parseInt(st.nextToken());
				
				if(intVal>0 && intVal<10)
					keyNumbers.add( (Integer) intVal );
			} catch(Exception e){ }
		}
		
		PhoneKey[] keyStrokes = new PhoneKey[keyNumbers.size()];
		
		for (int i = 0; i < keyStrokes.length; i++){
			keyStrokes[i] = PhoneKey.getPhoneKey(keyNumbers.get(i));
		}
		
		StringBuffer output = new StringBuffer();
		output.append("<html><head>");
		output.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
		output.append("<title>turkish-predictive-text demo</title>");
		output.append("</head><body>");
		
		output.append("<h1> Results </h1>");
		output.append("<p> Recognized keystrokes: ");
		for(int i : keyNumbers)
			output.append(" "+i+"("+getCharArrayAsString(PhoneKeyLetters.getKeyLetters(PhoneKey.getPhoneKey(i)))+") ");
		output.append(".</p>");
		
		long start = System.nanoTime();
		
		ArrayList<String> results = null, combinations = null;
		long possibleCombinations = 0;
		
		try{
			combinations = PredictionEngine.getFilteredCombinations(keyStrokes, Integer.MAX_VALUE);
			results = DictionaryValidator.validateAll(combinations);
			String[] frequencySortedResults = PredictionEngine.sortResultsByFrequency(results);
			
			possibleCombinations = PredictionEngine.getNumberOfCombinations(keyStrokes);

			output.append("<b>"+frequencySortedResults.length+" possible words are validated out of "+possibleCombinations+" possible combinations.</b><br /><br />");
			
			if(results.size() > 0){
				output.append("<b style=\"color:red\"> VALID COMBINATIONS: </b> (sorted by frequencies)<ul>");
				for(String word : results){
					output.append("<li>"+ word +"</li>");
				}
				output.append("</ul><br /><br />");
			}
		} catch(Exception e){
			output.append("<h1>Error occured. Try again. </h1>");
		}
		
		long end = System.nanoTime();
		
		
		output.append("<!--<small>This process took "+(end-start)*1e-9+" s. on Google App Engine servers :)</small><br /><br />-->");
		
		output.append("<a href=\"/\"> <b>Try again &gt;&gt;</b></a>");
		output.append("</body></html>");
		
		resp.getWriter().println(output);
		
		
	}
	
	String getCharArrayAsString(char[] c){
		StringBuffer out = new StringBuffer();
		for(char ch : c) out.append(ch);
		
		return out.toString();
	}
}
